package com.newegg.datafeed.adserving.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.newegg.datafeed.adserving.api.annotation.AccessRequired;
import com.newegg.datafeed.adserving.domain.User;
import com.newegg.datafeed.adserving.model.UserType;
import com.newegg.datafeed.adserving.request.UserForm;
import com.newegg.datafeed.adserving.response.Page;
import com.newegg.datafeed.adserving.response.Response;
import com.newegg.datafeed.adserving.response.UserInfo;
import com.newegg.datafeed.adserving.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="用户接口")
@RestController
@RequestMapping("/api/user")
public class UserAPI extends BaseAPI{
	
	@Autowired
	UserService service;
	
	@ApiOperation(value="帐号列表", consumes="application/json", produces="application/json")
    @RequestMapping(value="/page/{company}", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired(role={UserType.COMPANY})
    public Response<Page<User>> page(@PathVariable int company, 
    		@RequestParam(defaultValue="1") Integer pageIndex, @RequestParam(defaultValue="10") Integer pageSize) throws Exception {
		return SUCCESS(service.pageByCompany(company, pageIndex, pageSize));
    }
	
	@ApiOperation(value="帐号详情", consumes="application/json", produces="application/json")
    @RequestMapping(value="/full/{username}", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired(role={UserType.COMPANY})
    public Response<UserInfo> page(@PathVariable String username) throws Exception {
		return SUCCESS(service.full(username));
    }
	
	@ApiOperation(value="用户帐号保存", consumes="application/json", produces="application/json")
    @RequestMapping(value="/save", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.COMPANY})
    public Response<User> save(@RequestBody UserForm user) throws Exception {
		return SUCCESS(service.save(user));
    }
	
	@ApiOperation(value="用户帐号更新", consumes="application/json", produces="application/json")
    @RequestMapping(value="/update", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.COMPANY, UserType.USER})
    public Response<User> update(@RequestBody UserForm user) throws Exception {
		return SUCCESS(service.update(user));
    }
	
	@ApiOperation(value="删除", consumes="application/json", produces="application/json")
    @RequestMapping(value="/delete", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.COMPANY})
    public Response<Void> delete(@RequestParam String id) throws Exception {
		service.delete(id);
		return SUCCESS();
    }
}
